import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/login'
const Login = () => import('../components/login')
const Home = () => import('../components/home')
const Welcome = () => import('../components/welcome')
const Users = () => import('../components/users/users')
const Right = () => import('../components/power/right')
const Roles = () => import('../components/power/roles')
const Cate = () => import('../components/goods/cate')
const Params = () => import('../components/goods/params')
const List = () => import('../components/goods/List')
const Add = () => import('../components/goods/add')
const Order = () => import('../components/order/order')
const Report = () => import('../components/report/report')

// import Home from '../components/home'
// import Welcome from '../components/welcome'
// import Users from '../components/users/users'
// import Right from '../components/power/right'
// import Roles from '../components/power/roles'
// import Cate from '../components/goods/cate'
// import Params from '../components/goods/params'
// import List from '../components/goods/List'
// import Add from '../components/goods/add'
// import Order from '../components/order/order'
// import Report from '../components/report/report'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [{
      path: '/welcome',
      component: Welcome
    }, {
      path: '/users',
      component: Users
    }, {
      path: '/rights',
      component: Right
    }, {
      path: '/roles',
      component: Roles
    },
    {
      path: '/categories',
      component: Cate
    },
    {
      path: '/params',
      component: Params
    },
    {
      path: '/goods',
      component: List
    },
    {
      path: '/goods/add',
      component: Add
    },
    {
      path: '/orders',
      component: Order
    },
    {
      path: '/reports',
      component: Report
    }
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 从哪个路径来
  // next 放行路径
  // next()放行 next('/login') 强制跳转
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
